package com.wythe.mall.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.wythe.mall.R;
import com.wythe.mall.adapter.SearchKeyListAdapter;
import com.wythe.mall.utils.GotoActivity;
import com.wythe.mall.utils.SharePreferenceUtils;

import java.util.ArrayList;
import java.util.Set;

public class SearchActivity extends AppCompatActivity implements SearchKeyListAdapter.OnItemClickListener {
    private RecyclerView keyRecyclerView;
    private RecyclerView historyRecyclerView;

    private ArrayList<String> keyArrayList;
    private SearchKeyListAdapter keyAdapter;

    private ArrayList<String> historyArrayList;
    private SearchKeyListAdapter historyAdapter;
    private EditText searchead;
    private String keyWord = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        if (keyArrayList == null) {
            keyArrayList = new ArrayList<>();
        }
        initView();
    }

    public void initView() {
        findViewById(R.id.title_left_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ((TextView) findViewById(R.id.titleText)).setText("搜索");
        historyRecyclerView = (RecyclerView) findViewById(R.id.historyRecyclerView);
        keyAdapter = new SearchKeyListAdapter(keyArrayList);
        historyRecyclerView.setLayoutManager(new GridLayoutManager(this,4));
        historyRecyclerView.setAdapter(keyAdapter);
        keyAdapter.setOnItemClickListener(this);
        searchead = (EditText) findViewById(R.id.et_searchtext);

        searchead.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
                if ((actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_GO ||
                        actionId == EditorInfo.IME_ACTION_SEND ||
                        actionId == EditorInfo.IME_ACTION_NEXT ||
                        actionId == EditorInfo.IME_ACTION_SEARCH)
                        || (event != null && event.getAction() == 0 && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    keyWord = searchead.getText().toString().trim();
                    Bundle bundle = new Bundle();
                    bundle.putString("keyword",keyWord);
                    GotoActivity.gotoActiviy(SearchActivity.this,ListActivity.class,bundle);
                    return true;
                }
                return false;
            }
        });
    }

    public void doSearchQuery(String keyWord) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        keyArrayList.clear();
        Set<String> strings = (Set<String>) SharePreferenceUtils.readObject(this, "SEARCH_HISTORY");
        if (strings != null) {
            for (String string : strings) {
                keyArrayList.add(string);
            }
        }

        keyAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(int position, String key) {
        Bundle bundle = new Bundle();
        bundle.putString("keyword",key);
        GotoActivity.gotoActiviy(SearchActivity.this,ListActivity.class,bundle);
    }

    public void onSeachCancel(View v) {
        keyWord = "";
        searchead.setText(keyWord);
    }
}
